//index.js
//获取应用实例
var util = require('../../utils/MD5.js')  
var app = getApp()
  
Page({
  data: {
    // userName:'20398196850',
    // passWord:'CRI02082109602',
    userName: '',
    passWord:'',
    imageUrl: '/image/xiaai.png'
  },
  //事件处理函数
  resetBtnClick:function(e){
    this.setData({
      userName: '',
      passWord:''
    });
  },
  loginBtnClick: function (e) {
    wx.showLoading({
      title: '登录中....',
    })
    console.log(" test :" + JSON.stringify(e)+ "  test2:"+e.detail.value);

    //获得表单数据
    var that = this

    if (this.data.userName.length == 0 || 
    this.data.passWord.length == 0) {
    }

    var username = this.data.userName
    var password = this.data.passWord

    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;  

    var token = timestamp + "@13f@" + this.data.userName
    token = util.hexMD5(token);
    console.log(" test timestamp:" + timestamp + " token:" + token + " username :" +      this.data.userName + "passWord :" + this.data.passWord);
    wx.request({
      url:"https://www.gzcri.cn/api/auth/login.json",
      data:{
        code: timestamp,
        token:token,
        grm: this.data.userName,
        pass:this.data.passWord
      },
      header: { "content-type":"application/x-www-form-urlencoded"},
      method:"POST",
      success:function(res){
        console.log("json:"+JSON.stringify(res));
        var responseData = res.data;
        var code = responseData.result.code;
        var msgcn = responseData.result.msgcn;
        wx.hideLoading();
        if (code >= 1 ){
          wx.showModal({
            title: '提示',
            content: msgcn,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          })
        }else{
          console.log(" login success!" + sid);
          var sid = responseData.data.authInfo.sid;
          wx.setStorageSync('bumpdata', responseData.data)
          wx.setStorageSync('userName', username)
          wx.setStorageSync('passWord', password)
          wx.setStorageSync('token', token)
          wx.setStorageSync('sid', sid)
          wx.switchTab({
            url: '../index/index'
          })
        }
      },fail:function(res){
        wx.hideLoading();
        wx.showToast({
          title: '网络异常....',
        })
      }
    })
  },
  userNameInput:function(e){
    console.log(" test  userNameInput :" + JSON.stringify(e) + "  test2:" + e.detail.value);
    this.setData({
      userName:e.detail.value
    });
    console.log(" test  userNameInput :" + this.data.userName);
  },
  passWdInput:function(e){
    console.log(" test  passWdInput :" + JSON.stringify(e) + "  test2:" + e.detail.value);
    this.setData({
      passWord:e.detail.value
    })
  },
  onLoad: function () {
    console.log('onLoad.....')
    var that = this;
    //获取本地数据
    wx.getStorage({
      key: 'userName',
      success: function (res) {
        console.log('getstoreage userName:'+res.data);
        that.setData({ userName: res.data });
      }
    });
    wx.getStorage({
      key: 'passWord',
      success: function (res) {
        console.log('getstoreage passWord:'+res.data);
        that.setData({ passWord: res.data });
      }
    });
  }
})